package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.LuckyData;
import com.xx.pmis.model.util.LuckyBlue;
import com.xx.pmis.model.util.LuckyRed;
import com.xx.pmis.services.LuckyDataService;
import com.xx.pmis.web.forms.LuckyDataForm;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.LuckyBlueUtil;
import com.xx.pmis.web.util.LuckyRedUtil;

/**
 * @class 双色球Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class LuckyDataAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(PresentAction.class);

	LuckyDataService luckyDataService = getPMISServices().getLuckyDataService();

	// 查询所有双色球
	public ActionForward listLuckyData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = luckyDataService.getAllLuckyData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.setAttribute("luckyDataList", all);
		request.setAttribute("position", "双色球/历史记录");
		return mapping.findForward("listLuckyData");
	}

	// 新增
	public ActionForward addLuckyData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("luckyDate", CurrentDate.currentDate());
		request.setAttribute("year", CurrentDate.currentYear());
		request.setAttribute("position", "双色球/新增");
		return mapping.findForward("addLuckyData");
	}

	// 新增，处理方法
	public ActionForward addLuckyData_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LuckyDataForm f = (LuckyDataForm) form;

		String issueNumber = f.getIssueNumber(); // 期号
		String r1 = f.getR1(); // 红球号码1
		String r2 = f.getR2(); // 红球号码2
		String r3 = f.getR3(); // 红球号码3
		String r4 = f.getR4(); // 红球号码4
		String r5 = f.getR5(); // 红球号码5
		String r6 = f.getR6(); // 红球号码6
		String b1 = f.getB1(); // 蓝球号码1
		String totalMoney = f.getTotalMoney(); // 奖池滚存
		String luckyDate = f.getLuckyDate(); // 开奖日期

		LuckyData luckyData = new LuckyData();
		luckyData.setIssueNumber(issueNumber);
		luckyData.setR1(r1);
		luckyData.setR2(r2);
		luckyData.setR3(r3);
		luckyData.setR4(r4);
		luckyData.setR5(r5);
		luckyData.setR6(r6);
		luckyData.setB1(b1);
		luckyData.setTotalMoney(totalMoney);
		luckyData.setLuckyDate(luckyDate);

		luckyData.setLdDelFlag("0");// 删除标志初始化为‘0’
		try {
			luckyDataService.saveOrUpdateLuckyData(luckyData);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listLuckyData(mapping, form, request, response);
		return mapping.findForward("listLuckyData");
	}

	// 编辑
	public ActionForward editLuckyData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String ldID = request.getParameter("ldID");

		LuckyData luckyData = new LuckyData();
		try {
			luckyData = luckyDataService.getLuckyDataByLuckyDataID(ldID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("luckyData", luckyData);
		request.setAttribute("position", "双色球/编辑");
		return mapping.findForward("editLuckyData");
	}

	// 编辑，处理方法
	public ActionForward editLuckyData_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LuckyDataForm f = (LuckyDataForm) form;

		String ldID = f.getLdID();
		String issueNumber = f.getIssueNumber(); // 期号
		String r1 = f.getR1(); // 红球号码1
		String r2 = f.getR2(); // 红球号码2
		String r3 = f.getR3(); // 红球号码3
		String r4 = f.getR4(); // 红球号码4
		String r5 = f.getR5(); // 红球号码5
		String r6 = f.getR6(); // 红球号码6
		String b1 = f.getB1(); // 蓝球号码1
		String totalMoney = f.getTotalMoney(); // 奖池滚存
		String luckyDate = f.getLuckyDate(); // 开奖日期

		LuckyData luckyData = null;
		try {
			luckyData = luckyDataService.getLuckyDataByLuckyDataID(ldID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		luckyData.setIssueNumber(issueNumber);
		luckyData.setR1(r1);
		luckyData.setR2(r2);
		luckyData.setR3(r3);
		luckyData.setR4(r4);
		luckyData.setR5(r5);
		luckyData.setR6(r6);
		luckyData.setB1(b1);
		luckyData.setTotalMoney(totalMoney);
		luckyData.setLuckyDate(luckyDate);
		try {
			luckyDataService.saveOrUpdateLuckyData(luckyData);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		listLuckyData(mapping, form, request, response);
		return mapping.findForward("listLuckyData");
	}

	// 删除
	public ActionForward delLuckyData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LuckyData luckyData = null;
		LuckyDataForm f = (LuckyDataForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				luckyData = luckyDataService.getLuckyDataByLuckyDataID(ids[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				luckyDataService.deleteLuckyData(luckyData);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		listLuckyData(mapping, form, request, response);
		return mapping.findForward("listLuckyData");
	}

	// 明细
	public ActionForward detailLuckyData(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String ldID = request.getParameter("ldID");

		LuckyData luckyData = new LuckyData();
		try {
			luckyData = luckyDataService.getLuckyDataByLuckyDataID(ldID);
		} catch (Exception e) {
			log.info("根据礼品ID查询该礼品异常！" + e.toString());
		}

		request.setAttribute("luckyData", luckyData);
		request.setAttribute("position", "[双色球]-[明细]");
		return mapping.findForward("detailLuckyData");
	}

	// 查询所有双色球,蓝球
	public ActionForward listLuckyDataOfBlue(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		List<LuckyBlue> list = new ArrayList<LuckyBlue>();
		try {
			all = luckyDataService.getAllLuckyData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Iterator<?> it = all.iterator();
		while (it.hasNext()) {
			LuckyData ld = (LuckyData) it.next();
			LuckyBlue lb = new LuckyBlue();
			switch (Integer.parseInt(ld.getB1())) {
			case 1:
				lb = LuckyBlueUtil.GetLuckyBlueOfB01(ld);
				list.add(lb);
				break;
			case 2:
				lb = LuckyBlueUtil.GetLuckyBlueOfB02(ld);
				list.add(lb);
				break;
			case 3:
				lb = LuckyBlueUtil.GetLuckyBlueOfB03(ld);
				list.add(lb);
				break;
			case 4:
				lb = LuckyBlueUtil.GetLuckyBlueOfB04(ld);
				list.add(lb);
				break;
			case 5:
				lb = LuckyBlueUtil.GetLuckyBlueOfB05(ld);
				list.add(lb);
				break;
			case 6:
				lb = LuckyBlueUtil.GetLuckyBlueOfB06(ld);
				list.add(lb);
				break;
			case 7:
				lb = LuckyBlueUtil.GetLuckyBlueOfB07(ld);
				list.add(lb);
				break;
			case 8:
				lb = LuckyBlueUtil.GetLuckyBlueOfB08(ld);
				list.add(lb);
				break;
			case 9:
				lb = LuckyBlueUtil.GetLuckyBlueOfB09(ld);
				list.add(lb);
				break;
			case 10:
				lb = LuckyBlueUtil.GetLuckyBlueOfB10(ld);
				list.add(lb);
				break;
			case 11:
				lb = LuckyBlueUtil.GetLuckyBlueOfB11(ld);
				list.add(lb);
				break;
			case 12:
				lb = LuckyBlueUtil.GetLuckyBlueOfB12(ld);
				list.add(lb);
				break;
			case 13:
				lb = LuckyBlueUtil.GetLuckyBlueOfB13(ld);
				list.add(lb);
				break;
			case 14:
				lb = LuckyBlueUtil.GetLuckyBlueOfB14(ld);
				list.add(lb);
				break;
			case 15:
				lb = LuckyBlueUtil.GetLuckyBlueOfB15(ld);
				list.add(lb);
				break;
			case 16:
				lb = LuckyBlueUtil.GetLuckyBlueOfB16(ld);
				list.add(lb);
				break;
			default:
				break;
			}

		}
		request.setAttribute("dataBlueList", list);
		request.setAttribute("position", "双色球/蓝球");
		return mapping.findForward("listDataBlue");
	}

	// 查询所有双色球,红球
	public ActionForward listLuckyDataOfRed(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = luckyDataService.getAllLuckyData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<LuckyRed> list = LuckyRedUtil.luckyDataOfRed(all);
		// 出现总次数
		int[] n = LuckyRedUtil.totalNumberOfLuckyRed(list);
		for (int i = 1; i <= n.length; i++) {
			request.setAttribute("n" + i, n[i - 1]);
		}
		request.setAttribute("dataRedList", list);
		request.setAttribute("position", "双色球/红球");
		return mapping.findForward("listDataRed");
	}
}